/**
 *
 */
define(['jquery','bbcare','echarts','dm/vm/oxygenPulseFequencyData'],function($,$bb,echarts,$oxygen){
    var oxygenPulseFequency = {};
    //秒数转换为时分秒：
    function hms(totals){
        var h=parseInt(totals/3600);
        var m=parseInt(totals%3600/60);
        var s=parseInt(totals%3600%60);
        h=h<10?"0"+h:h;
        m=m<10?"0"+m:m;
        s=s<10?"0"+s:s;
        if(m==0 && s==0){
            var time=Number(h);
        }else{
            time=h+":"+m+":"+s;
        }
        return time
    }
    //时分秒转换为小时：
    function toS(val){
        var tt=val;
        var h=tt.substring(11,13);
        var m=tt.substring(14,16);
        var s=tt.substring(17);
        var time=Number(h)*3600+Number(m)*60+Number(s);
        return time;
    }
    function dataDeal(dataArr){
        var valO=[];
        var valP=[];
        for(var i=0;i<dataArr.length;i++){
            for(var j=0;j<dataArr[i].length;j++){
                if(j=="0"){
                    var testTime=toS(dataArr[i][j].testTime);
                    var plusRate=[testTime-0.1,"-"];
                    var oxygen=[testTime-0.1,"-"];
                    valO.push(oxygen);
                    valP.push(plusRate);
                }
                 testTime=toS(dataArr[i][j].testTime);
                 plusRate=[testTime,dataArr[i][j].pulseRate];
                 oxygen=[testTime,dataArr[i][j].oxygensaturation];
                 valO.push(oxygen);
                 valP.push(plusRate);
               if(j==dataArr[i].length-1){
                    testTime=toS(dataArr[i][j].testTime);
                    plusRate=[testTime+0.1,"-"];
                    oxygen=[testTime+0.1,"-"];
                    valO.push(oxygen);
                    valP.push(plusRate);
                }
            }
        }
        return {
            "p":valP,
            'o':valO
        };
    }
    oxygenPulseFequency.init = function(){
        var URL = $bb.baseUrl("department")+"followup/service";
        var objInfo=$bb.getPageParams();
        var userId=objInfo.userId;
        $bb.ajax({
            url: URL + "/patient/oxygenPulserate/historyByDay",
            type:"post",
            contentType: "application/json;charset=UTF-8",
            dataType:"json",
            data:JSON.stringify({
                data : {
                    "userId": userId
                }
            }),
            success: function(data){
                $oxygen.listDayInit({
                    id:"oxygenlistBox",
                    list:data.rows
                });
                $(".content-three").css("display","none");
                $("#detailInfo").html("").css({"height":"400px","padding":"10px 0"});
                $("#timeSharingStatistic").html("");
                $("#trend").html("");
                $("#oxygenlistBox .bottom-con").eq(0).addClass("active");
                var testTime= data.rows[0].testTime;
                detail(testTime,userId);
                $("#oxygenlistBox .bottom-con").unbind("click");
                $("#oxygenlistBox .bottom-con").click(function(){
                    $(this).addClass("active").siblings(".bottom-con").removeClass("active");
                    var testTime = $(this).html();
                    detail(testTime,userId);
                });
                function detail(testTime,userId){
                    $bb.ajax({
                        url: URL + "/patient/oxygenPulserate/detailByDay",
                        type:"post",
                        contentType: "application/json;charset=UTF-8",
                        dataType:"json",
                        data:JSON.stringify({
                            data : {
                                "testTime": testTime,
                                "userId":userId
                            }
                        }),
                        success: function(data){
                            var oxygenPulserateDetails = data.data.oxygenPulserateDetails;
                            var res=dataDeal(oxygenPulserateDetails);
                            trend(res);
                            //下方折线图
                            function trend(obj){
                                var myChart = echarts.init(document.getElementById('detailInfo'));
                                var option = {
                                    title : {},
                                    grid: {
                                        bottom: 80
                                    },
                                    tooltip : {
                                        show:"true",
                                        trigger: 'axis',
                                        snap:false,
                                        axis:"x",
                                        axisPointer: {
                                            type: 'line',
                                            animation: false,
                                            label: {
                                                backgroundColor: '#505765'
                                            }
                                        },
                                        formatter: function (params,ticket,callback) {
                                            var val1='';
                                            for(var i=0;i<params.length;i++){
                                                if(params[i].data[1]!="-"){
                                                    val1+=params[i].seriesName+":"+hms(params[i].data[0])+"&nbsp;"+params[i].data[1]+"<br/>";
                                                }else{
                                                    val1+=params[i].seriesName+":&nbsp;无记录"+"<br/>";
                                                }
                                            }
                                            return val1;
                                        }
                                    },
                                    legend: {
                                        data:['氧饱','脉率']
                                    },
                                    dataZoom: [
                                        {
                                            type:"slider",
                                            show: true,
                                            realtime: true,
                                            start: 0,
                                            end: 100,
                                            labelFormatter:function(value){
                                                return hms(value);
                                            },
                                            filterMode: 'empty'
                                        },
                                        {
                                            type: 'inside',
                                            realtime: true,
                                            start: 0,
                                            end: 100,
                                            filterMode: 'empty'
                                        }
                                    ],
                                    xAxis : [
                                        {
                                            type : 'value',
                                            boundaryGap : false,
                                            axisLine: {onZero: false},
                                            min:0,
                                            max:86400,
                                            interval:3600,
                                            splitLine:{
                                                show: false//保留网格线
                                            },
                                            axisLabel:{
                                                formatter:function(value,index){
                                                    return hms(value);
                                                }
                                            }
                                        }
                                    ],
                                    yAxis:{
                                        splitLine:{
                                            show: true//保留网格线
                                        },
                                        type: 'value'
                                    },
                                    series: [
                                        {
                                            name:'氧饱',
                                            type:'line',
                                            symbol:"none",
                                            animation: false,
                                            smooth:true,
                                            areaStyle: {
                                                normal: {
                                                    color:"transparent"
                                                }
                                            },
                                            itemStyle : {
                                                normal : {
                                                    color:'#fc5d26',
                                                    lineStyle:{
                                                        color:'#fc5d26'
                                                    }
                                                }
                                            },
                                            data:obj.o
                                        },
                                        {
                                            name:'脉率',
                                            type:'line',
                                            symbol:"none",
                                            smooth:true,
                                            animation: false,
                                            areaStyle: {
                                                normal: {
                                                    color:"transparent"
                                                }
                                            },
                                            itemStyle : {
                                                normal : {
                                                    color:'#119BA9',
                                                    lineStyle:{
                                                        color:'#119BA9'
                                                    }
                                                }
                                            },
                                            data:obj.p
                                        }
                                    ]
                                };
                                myChart.setOption(option);
                            }
                        }
                    });
                }
            }
        });
        //左边内容模块第一个折线图
        $(function(){
            $(window).resize(function(){
                var width=$("#detailInfo").width();
                $("#detailInfo >  div").css({"width":width});
                $("#detailInfo >  div > canvas").css({"width":width});
            });
        });
    }
    return oxygenPulseFequency;
});